<?php
/**
 * Joomla! 1.5 component wbdatabase
 *
 * @version $Id: wbdatabase.php 2009-04-09 16:20:44 svn $
 * @author woomla
 * @package Joomla
 * @subpackage wbdatabase
 * @license GNU/GPL
 *
 * A personal database with some fields to remember logins and pin codes.
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');


/*==================================================================================================
  Description
  
  20090416      Wim       Created
==================================================================================================*/
class WbdatabasesModelWbdatabase extends JModel  
{
  private $id,
          $data,
          $query,
          $description;
  

/*==================================================================================================
  Description
  
  20090416      Wim       Created
==================================================================================================*/
  function __construct()  
  {
    parent::__construct();
    
    $array = JRequest::getVar('cid',  0, '', 'array');
    $this->setId((int)$array[0]);
  }
  

/*==================================================================================================
  Description
  
  20090416      Wim       Created
==================================================================================================*/
  function setId($id)
  {
    $this->id = $id;
    $this->data = null;
    $this->query = null;
  }
  

/*==================================================================================================
  Description
  
  20090416      Wim       Created
==================================================================================================*/
  function buildQuery()
  {
    $this->query = 'SELECT * FROM #__wbdatabase' .
             ' WHERE id = ' . $this->id;
  }
  

/*==================================================================================================
  Description
  
  20090602      Wim       Created
==================================================================================================*/
  function buildDescribeQuery()
  {
    $this->query = 'DESCRIBE #__wbdatabase';    
  }
  

/*==================================================================================================
  Description
  
  20090416      Wim       Created
==================================================================================================*/
  function &getData()
  {
    if (empty($this->data))
    {
      $this->buildQuery();
			$this->_db->setQuery( $this->query );
			$this->data = $this->_db->loadObject();
    }
		if (!($this->data))
    {
			$this->data = new stdClass();
//			$this->data->id       = 0;
//			$this->data->title    = null;
//			$this->data->code     = null;
//			$this->data->user     = null;
//			$this->data->email    = null;
//			$this->data->url      = null;
//			$this->data->comment  = null;
//			$this->data->tags     = null;

      $this->buildDescribeQuery();
      $this->description = $this->_getList($this->query);
      foreach($this->description as $row)
      {
        $this->data->{$row->Field} = null;
      }
		}
		return $this->data;
  }
  
  function &getId()
  {
    return $this->id;
  }
  
  function &getQuery()
  {
    return $this->query;
  }
  
  function &getDescription()
  {
    return $this->description;
  }


/*==================================================================================================
  Description
  
  20090416      Wim       Created
==================================================================================================*/
  function store()
  {
    $row =& $this->getTable();
    
    $data = JRequest::get('post');
    
    if (!$row->bind($data))
    {
      $this->setError($this->_db->getErrorMsg());
      return false;
    }
    
    if (!$row->check())
    {
      $this->setError($this->_db->getErrorMsg());
      return false;
    }
    
    if (!$row->store())
    {
      $this->setError($this->_db->getErrorMsg());
      return false;
    }
    return true;
  }


/*==================================================================================================
  Description
  
  20090416      Wim       Created
==================================================================================================*/
  function delete()
  {
    $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
    $row =& $this->getTable();
 
    foreach($cids as $cid) 
    {
      if (!$row->delete( $cid )) 
      {
        $this->setError( $row->getErrorMsg() );
        return false;
      }
    }
    return true;
  }
  
}
?>